package org.zero.codec.advice;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdviceAdapter;
import org.zero.codec.annotation.Decrypt;
import org.zero.codec.annotation.Encrypt;
import org.zero.codec.config.CodecProperties;
import org.zero.codec.util.AesUtil;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.util.Optional;

/**
 * @author Zero (cnzeropro@qq.com)
 * @date 2022/11/29
 */
@EnableConfigurationProperties(CodecProperties.class)
@ControllerAdvice
public class DecryptRequest extends RequestBodyAdviceAdapter {
    @Resource
    private CodecProperties codecProperties;

    @Override
    public boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
        return methodParameter.hasMethodAnnotation(Decrypt.class) || methodParameter.hasParameterAnnotation(Decrypt.class);
    }

    @Override
    public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter,
                                           Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
        InputStream in = inputMessage.getBody();
        byte[] body = new byte[in.available()];
        if (in.read(body) != -1) {
            String key = Optional.ofNullable(parameter.getMethodAnnotation(Encrypt.class)).map(Encrypt::key).filter(StringUtils::hasText).orElse(codecProperties.getKey());
            byte[] decrypt = AesUtil.decrypt(body, key);
            return new HttpInputMessage() {
                @Override
                public InputStream getBody() throws IOException {
                    return new ByteArrayInputStream(decrypt);
                }

                @Override
                public HttpHeaders getHeaders() {
                    return inputMessage.getHeaders();
                }
            };
        }

        return super.beforeBodyRead(inputMessage, parameter, targetType, converterType);
    }
}
